<template>
  <div class="role-management">
    <h1>角色管理</h1>
    <el-button type="primary" @click="showAddRoleDialog">添加角色</el-button>
    <el-table :data="roles" style="width: 100%">
      <el-table-column prop="id" label="角色ID" width="180"></el-table-column>
      <el-table-column prop="name" label="角色名称" width="180"></el-table-column>
      <el-table-column prop="description" label="描述" width="180"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="showEditRoleDialog(scope.row)">编辑</el-button>
          <el-button @click="deleteRole(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加角色" :visible.sync="addRoleDialogVisible">
      <el-form :model="newRole">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="newRole.name"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="newRole.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRoleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="编辑角色" :visible.sync="editRoleDialogVisible">
      <el-form :model="editRole">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="editRole.name"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="editRole.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editRoleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, addRole, updateRole, deleteRole } from '@/api/role';

export default {
  data() {
    return {
      roles: [],
      addRoleDialogVisible: false,
      editRoleDialogVisible: false,
      newRole: {
        name: '',
        description: '',
      },
      editRole: {},
    };
  },
  methods: {
    async fetchRoles() {
      const response = await getRoleList();
      this.roles = response.data;
    },
    showAddRoleDialog() {
      this.newRole = { name: '', description: '' };
      this.addRoleDialogVisible = true;
    },
    async addRole() {
      await addRole(this.newRole);
      this.addRoleDialogVisible = false;
      this.fetchRoles();
    },
    showEditRoleDialog(role) {
      this.editRole = { ...role };
      this.editRoleDialogVisible = true;
    },
    async updateRole() {
      await updateRole(this.editRole.id, this.editRole);
      this.editRoleDialogVisible = false;
      this.fetchRoles();
    },
    async deleteRole(id) {
      await deleteRole(id);
      this.fetchRoles();
    },
  },
  mounted() {
    this.fetchRoles();
  },
};
</script>

<style scoped>
.role-management {
  padding: 20px;
}
</style>